package Review;

import java.util.Scanner;

public class Dfs {
    /*
5 4
1 1 2 1
1 1 1 1
1 1 2 1
1 2 1 1
1 1 1 2
1 1 4 3
     */

    static int m;
    static int n;
    static int p;
    static int q;
    static int min = 999;
    //a中1表示空地,2表示障碍物
    static int[][] a = new int[100][100];
    //0表示未访问,1表示访问
    static int[][] v = new int[100][100];
    //定义方向 , 顺时针 ,右下左上
    static int[] dx = {0,1,0,-1};
    static int[] dy = {1,0,-1,0};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        m = sc.nextInt();
        n = sc.nextInt();
        for(int i = 1;i<=m;i++){
            for (int j = 1;j<=n;j++){
                a[i][j] = sc.nextInt();
            }
        }
        int startx = sc.nextInt();
        int starty = sc.nextInt();
        p = sc.nextInt();
        q = sc.nextInt();
        v[startx][starty] = 1;
        dfs2(startx,starty,0);
        System.out.println(min);
    }
    public static void dfs(int x,int y,int step){
        if(x == p && y == q){
            if(step < min){
                min = step;
            }
            //更新最小值之后还要进行回退
            return;
        }
        //当前位置不是终点的时候需要在四个方向进行试探
        for (int k = 0;k<4;k++){
            int tx = 0,ty = 0;
            tx = x + dx[k];
            ty = y + dy[k];
            if(a[tx][ty] == 1 && v[tx][ty] ==0){
                v[tx][ty] = 1;
                dfs(tx,ty,step+1);
                v[tx][ty] = 0;
            }

        }
        //整个过程执行完进行回退
        return;
    }

    public static void dfs2(int x,int y,int step){
        if(x == p && y == q){
            if(step < min) min = step;
            return;
        }
        //遍历四个方向
        for(int i = 0;i<4;i++){
            int tx = x + dx[i];
            int ty = x + dy[i];
            if(a[tx][ty] == 1 && v[tx][ty] == 0){
                v[tx][ty] = 1;
                dfs(tx,ty,step+1);
                v[tx][ty] = 0;
            }
        }
    }



}
